package com.ljfiction.book.Dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.http.UserManageBean;
import com.ljfiction.book.tools.Tool;

import java.util.HashMap;
import java.util.Objects;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.moments.WechatMoments;

public class ToShareDialog {
    private String bookId, bookName, userId, url, lianjieurl;
    private Context mContext;
    private Dialog mDialog;
    private Listener listener;
    private LinearLayout qqZone, wxPengyouquan, sinlangweibo;

    /*
     * 1是读者，2是作者
     * */
    public ToShareDialog(Context context, String bookid, String bookname, String url, String userId, int type) {
        mContext = context;
        this.bookId = bookid;
        this.bookName = bookname;
        this.userId = userId;
        this.url = url;
        lianjieurl = HttpManager.ShareIP + "/mob/appShare?bookId=" + bookId + "&type=" + type + "&userCode=LGYQ001" + userId;
        mDialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        @SuppressLint("InflateParams") View view = LayoutInflater.from(context).inflate(R.layout.share_three, null);
        qqZone = view.findViewById(R.id.share_qzone);
        qqZone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!UserManageBean.isLogin(mContext)) {
                    return;
                }
                toshareone(1);
            }
        });
        sinlangweibo = view.findViewById(R.id.share_sina);
        sinlangweibo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!UserManageBean.isLogin(mContext)) {
                    return;
                }
                toshareone(0);
            }
        });
        wxPengyouquan = view.findViewById(R.id.share_wechat);
        wxPengyouquan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!UserManageBean.isLogin(mContext)) {
                    return;
                }
                toshareone(2);
            }
        });
        //将布局设置给Dialog
        mDialog.setContentView(view);
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.width = context.getResources().getDisplayMetrics().widthPixels;
        view.setLayoutParams(layoutParams);
        Objects.requireNonNull(mDialog.getWindow()).setGravity(Gravity.BOTTOM);
        mDialog.getWindow().setWindowAnimations(R.style.AnimBottom);
        mDialog.setCancelable(true);
        intView();
    }

    private void intView() {

    }

    public void setListener(Listener listener) {
        this.listener = listener;

    }


    public interface Listener {
        void toRefreshi();
    }

    public void show() {
        if (mDialog != null && !mDialog.isShowing()) {
            mDialog.show();
        }
    }

    private void toshareone(int type) {

        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        switch (type) {
            case 0:
                oks.setPlatform(SinaWeibo.NAME);
                break;
            case 1:
                oks.setPlatform(QZone.NAME);
                break;
            case 2:
                oks.setPlatform(WechatMoments.NAME);
                break;
            case 3:

                break;
        }
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle("《" + bookName + "》");

        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用

        oks.setTitleUrl(lianjieurl);

        // text是分享文本，所有平台都需要这个字段
        oks.setText("我最近在追这本小说，来了解一下？");
        oks.setImageUrl(url);

        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        // oks.setImagePath(TEST_IMAGE);//确保SDcard下面存在此张图片

        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(lianjieurl);
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我最近在追这本小说，来了解一下？");
        oks.setCallback(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                Tool.showToast("分享成功", mContext);
                listener.toRefreshi();
                mDialog.dismiss();
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                Tool.showToast("分享错误", mContext);
            }

            @Override
            public void onCancel(Platform platform, int i) {
                Tool.showToast("分享取消", mContext);
            }
        });
        // site是分享此内容的网站名称,仅在QQ空间使用
        oks.setSite(mContext.getString(R.string.app_name));

        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(lianjieurl);

        // 启动分享GUI
        oks.show(mContext);

    }
}
